package com.chaosj.memory;

import java.util.ServiceLoader;

/**
 * @className: CmdParserHandler
 * @description:
 * @author: caoyangjie
 * @date: 2021/8/10
 **/
public class CmdParserHandler {

    public static final String HELP = "help";

    /**
     * 根据命令行执行响应操作
     * @param args
     */
    public void execute(String[] args, StringBuffer stringBuffer){
        if ( args==null || args.length==0 ) {
           args = new String[]{HELP};
        }
        String[] finalArgs = args;
        ServiceLoader.load(IToolHandler.class).forEach(handler->{
            if ( HELP.equalsIgnoreCase(finalArgs[0]) ) {
                stringBuffer.append(handler.description());
                stringBuffer.append("\n");
            }
            if ( handler.support(finalArgs[0]) ){
                handler.round(stringBuffer, true);
                handler.handler(handler.cmd2Dto(finalArgs), stringBuffer);
                handler.round(stringBuffer, false);
            }
        });
    }
}
